import urlConfig from '../axios/config.js'
import { taskStatus, orderStatus, PayStatus, DefStatus,oPassStatus } from './setting'
import qqmapsdk from '@/util/qqmap-wx-jssdk.min.js'
const QQMapWX = new qqmapsdk({
    key: 'KF6BZ-5SULX-U6K47-ZHTG5-MGVL5-FAB7D',
})
export function getCurPage () {
    let pages = getCurrentPages();
    let curPage = pages[pages.length - 1];
    return curPage
}
export const api = {
    handleBackTo () {
        uni.navigateBack()
    },
    handleHomeTo () {
        uni.switchTab({
            url: '/pages/tools/tools'
        })
    },
    previewImgs (ary, url) {
        uni.previewImage({
            current: url, // 当前显示图片的http链接
            urls: ary // 需要预览的图片http链接列表
        })
    },
    modelMessage (msg, content) {
        return new Promise((resolve, reject) => {
            uni.showModal({
                title: msg,
                content: content,
                success (res) {
                    if (res.confirm) {
                        resolve()
                    } else {
                        reject()
                    }
                }
            })
        })

    },
    navigator (url) {
        uni.navigateTo({
            url
        });
    },
    getreqImg (list) {
        return list.map((x) => x.response.Value[0].sFileUrl).join(',');
    },
    getImg (str) {
        let img = str.replaceAll('\\', '/')
        return img.substr(1)
    },
    getUpload (img, cb) {
        uni.uploadFile({
            url: urlConfig + '/api/Upfile/PostImgFile',
            filePath: img,
            name: 'file',
            header: { token: uni.getStorageSync('token') },
            fileType: 'image',
            success: (data) => {
                let files = JSON.parse(data.data).Value
                uni.showModal({
                    content: '上传成功',
                    showCancel: false,
                })
                cb && cb(files[0].sFileUrl)
            }
        })
    },
    getUserLoacation (callback) {
        var isUserLocation = uni.getStorageSync("isUserLocation");
        if (!isUserLocation) {
            console.log("来了")
            uni.getLocation({
                type: 'gcj02',
                altitude: true, //高精度定位
                //定位成功，更新定位结果
                success: function (location) {
                    QQMapWX.reverseGeocoder({
                        location: {
                            latitude: location.latitude,
                            longitude: location.longitude,
                        },
                        coord_type: 1,
                        success: function (res) {
                            if (callback) {
                                callback(Object.assign(res, location));
                            }

                        },
                    })

                },
                //定位失败回调
                fail: function () {
                    uni.getSetting({
                        success: function (e) {
                            if (!e.authSetting['scope.userLocation']) {
                                uni.showToast({
                                    title: '请允许使用地理位置信息',
                                    icon: "none"
                                })
                                uni.setStorageSync("isUserLocation", true);
                            } else {
                                //用户已授权，但是获取地理位置失败，提示用户去系统设置中打开定位
                                uni.showModal({
                                    title: '',
                                    content: '请在手机系统设置中打开GPS定位服务',
                                    showCancel: false,
                                    success: function (res) {
                                        if (callback) {
                                            callback();
                                        }
                                    }
                                })

                            }
                        }
                    })
                }
            })
        } else {
            uni.openSetting({
                success (even) {
                    if (!even.authSetting['scope.userLocation']) {
                        uni.showToast({
                            title: '请允许使用地理位置信息',
                            icon: "none"
                        })
                    } else {
                        uni.setStorageSync("isUserLocation", false);
                        if (callback) {
                            callback();
                        }
                    }

                }
            })
        }

    },
    handleBack (text, delta = 1) {
        uni.showToast({
            title: text,
            duration: 2000
        });
        setTimeout(() => {
            uni.navigateBack({
                delta
            });
        }, 1500)
    },
    handleToUrl (text, url) {
        uni.showToast({
            title: text,
            duration: 2000
        });
        setTimeout(() => {
            uni.navigateTo({
                url
            });
        }, 1500)
    },
    switchTabToUrl (text, urlTo) {
        uni.showToast({
            title: text,
            duration: 2000
        });
        setTimeout(() => {
            uni.switchTab({
                 url: urlTo
            });
        }, 1500)
    },

    UserImg (img) {
        var timestamp1 = Date.parse(new Date());
        return `${urlConfig}/images/user/${img}.jpg`
    },
    MrkImg (img) {
        var timestamp1 = Date.parse(new Date());
        return `${urlConfig}/images/mrk/${img}.jpg`
    },
    OrderImg (img) {
        return `${urlConfig}/images/product/${img}.jpg`
    },
    ProImg (img) {
        var timestamp1 = Date.parse(new Date());
        return `${urlConfig}/images/product/${img}.jpg`
    },
	FileImg (img) {
	    var timestamp1 = Date.parse(new Date());
	    return `${urlConfig}${img}?id=` + timestamp1
	},
	
    getTaskStatus (status) {
        return taskStatus[status]
    },
    getOrderStatus (status) {
        return orderStatus[status]
    },
    getPayStatus (status) {
        return PayStatus[status]
    },
    getDefStatus (status) {
        return DefStatus[status]
    },
	getApplyProStatus (status) {
	    return ApplyProStatus[status]
	},
	getPassStatus (status) {
	    return oPassStatus[status]
	},
    getCheck (status) {
        let form = new Map([[0, { name: '待提交', color: "#FF930A" }], [1, { name: '审核中', color: '#FF930A' }], [2, { name: '审核通过', color: "#22BFB6" }], [3, { name: '审核不通过', color: "#f00" }]])
        return form.get(status)
    }
}